import {reactive} from "vue";
import {getOptionsOf} from "../../api/options";
import {useStore} from "vuex";
import {CSOptionsDAT} from "../../types/responseTypes";

interface CuPickerOpt {
    value: string
    label: string
}

interface ProfileOpts {
    [key: string]: CuPickerOpt[]
}

interface ProfileArg {
    CSNAME: string
    key: string
}

function transferCSOption(source: CSOptionsDAT[]): CuPickerOpt[]{
    return source.map(item => ({
        value: item.CSID + '',
        label: item.CSVALUE
    }))
}

export function useProfileArgs(args: ProfileArg[] = []){
    const {CUSER_ID} = useStore().state.user.userInfo
    const opts = reactive<ProfileOpts>({})
    args.forEach(arg => {
        opts[arg.key] = []
    })
    // const ZZMMOpts = ref<CuPickerOpt[]>([])
    // const WHCDOpts = ref<CuPickerOpt[]>([])
    // const sexOpt = ref<CuPickerOpt[]>([])

    // Promise.all([getOptionsOf('政治面貌',CUSER_ID),getOptionsOf('文化程度',CUSER_ID),getOptionsOf('性别',CUSER_ID)]).then(res => {
    //     ZZMMOpts.value = transferCSOption(res[0].DAT)
    //     WHCDOpts.value = transferCSOption(res[1].DAT)
    //     sexOpt.value = transferCSOption(res[2].DAT)
    // }).catch(() => {
    //     uni.showToast({
    //         title: '获取参数失败，请稍候再试',
    //         icon: 'none',
    //         mask: true
    //     })
    // })
    Promise.all(args.map(arg => getOptionsOf(arg.CSNAME,CUSER_ID))).then(res => {
        res.forEach((item,idx) => {
            opts[args[idx].key].push(...transferCSOption(res[idx].DAT))
        })
    }).catch((err) => {
        uni.showToast({
            title: '获取参数失败，请稍候再试',
            icon: 'none',
            mask: true
        })
    })
    return {
        // ZZMMOpts,
        // WHCDOpts,
        // sexOpt
        opts
    }
}
